package com.jianxiu.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jianxiu.bean.BaseController;
import com.jianxiu.bean.Cons;
import com.jianxiu.bean.OpView;
import com.jianxiu.bean.Result;
import com.jianxiu.bean.User;
import com.jianxiu.bean.task.Report;
import com.jianxiu.service.SysConfigService;
import com.jianxiu.service.TaskService;
import com.jianxiu.service.UserService;

@Controller
@RequestMapping("task")
public class TaskController extends BaseController {

    @Resource
    private TaskService taskService;
    @Resource
    private UserService userService;
    @Resource
    private SysConfigService sysConfigService;

    @RequestMapping("toReport")
    public String toReport(HttpServletRequest request, Model model) {
        User user = getSessionUser(request);
        if (!user.isRegister()) return "forward:/redirect.do?to=register";
        //报修项目
        model.addAttribute("areaList", sysConfigService.buildDepArea());
        //报修地点
        model.addAttribute("projectList", sysConfigService.listByType("PROJECT"));
        return "report-task";
    }

    @RequestMapping("report")
    @ResponseBody
    public Result report(HttpServletRequest request, Report report) {
        User user = getSessionUser(request);
        if (user == null) return Result.byMsg("请重新登录");
        report.setUserId(user.getId());
        return taskService.report(report);
    }

    //foot 下面的待办
    @RequestMapping("wait/list")
    public String listWait(HttpServletRequest request, Model model) {
        User user = getSessionUser(request);
        List<Report> reports = taskService.listWait(user.getId());
        model.addAttribute("reports", reports);
        model.addAttribute("title", "我的待办");
        model.addAttribute("header", "待办");
        model.addAttribute("from", "waitList");
        return "task";
    }

    //foot下面的已办
    @RequestMapping("over/list")
    public String listOver(HttpServletRequest request, Model model) {
        User user = getSessionUser(request);
        List<Report> reports = taskService.listOver(user.getId());
        model.addAttribute("reports", reports);
        model.addAttribute("title", "我的已办");
        model.addAttribute("header", "已办");
        model.addAttribute("from", "overList");
        return "task";
    }

    //待审核
    @RequestMapping("wait/sure")
    public String waitSure(HttpServletRequest request, Model model) {
        User user = getSessionUser(request);
        if (!user.isAdmin()) return Cons.ERROR_PAGE;
        List<Report> reports = taskService.waitSure();
        if (CollectionUtils.isEmpty(reports)) return "forward:/home.do";
        model.addAttribute("reports", reports);
        model.addAttribute("title", "待管理员审核");
        model.addAttribute("header", "去审核");
        model.addAttribute("from", "toSure");
        return "task";
    }

    //待维修
    @RequestMapping("wait/main")
    public String waitMain(HttpServletRequest request, Model model) {
        User user = getSessionUser(request);
        if (!user.isAdmin()) return Cons.ERROR_PAGE;
        List<Report> reports = taskService.waitMain();
        if (CollectionUtils.isEmpty(reports)) return "forward:/home.do";
        model.addAttribute("reports", reports);
        model.addAttribute("title", "待维修人员维修");
        model.addAttribute("header", "待维修");
        model.addAttribute("from", "toMain");
        return "task";
    }

    //待办结
    @RequestMapping("wait/over")
    public String waitOver(HttpServletRequest request, Model model) {
        User user = getSessionUser(request);
        if (!user.isAdmin()) return Cons.ERROR_PAGE;
        List<Report> reports = taskService.overMaintain();
        if (CollectionUtils.isEmpty(reports)) return "forward:/home.do";
        model.addAttribute("reports", reports);
        model.addAttribute("title", "待管理人员办结");
        model.addAttribute("header", "去办结");
        model.addAttribute("from", "toOver");
        return "task";
    }

    //已办结
    @RequestMapping("over")
    public String over(HttpServletRequest request, Model model) {
        User user = getSessionUser(request);
        if (!user.isAdmin()) return Cons.ERROR_PAGE;
        List<Report> reports = taskService.overList();
        if (CollectionUtils.isEmpty(reports)) return "forward:/home.do";
        model.addAttribute("reports", reports);
        model.addAttribute("title", "已办结");
        model.addAttribute("header", "已办结");
        model.addAttribute("from", "over");
        return "task";
    }

    @RequestMapping("wait/my/maintain")
    public String waitMyMaintain(HttpServletRequest request, Model model) {
        User user = getSessionUser(request);
        if (!user.isWork()) return Cons.ERROR_PAGE;
        List<Report> reports = taskService.getByMainId(user.getId(), new int[]{1});
        if (CollectionUtils.isEmpty(reports)) return "forward:/home.do";
        model.addAttribute("reports", reports);
        model.addAttribute("title", "我的待修");
        model.addAttribute("header", "去维修");
        model.addAttribute("from", "toMyMain");
        return "task";
    }

    @RequestMapping("wait/my/over")
    public String waitMyOver(HttpServletRequest request, Model model) {
        User user = getSessionUser(request);
        if (!user.isWork()) return Cons.ERROR_PAGE;
        List<Report> reports = taskService.getByMainId(user.getId(), new int[]{2, 9});
        if (CollectionUtils.isEmpty(reports)) return "forward:/home.do";
        model.addAttribute("reports", reports);
        model.addAttribute("title", "我的已维修");
        model.addAttribute("header", "已维修");
        model.addAttribute("from", "toMyOver");
        return "task";
    }

    @RequestMapping("show")
    public String listLog(HttpServletRequest request, Model model, Long reportId, String from) {
        User user = getSessionUser(request);
        Report report = taskService.getById(reportId);
        model.addAttribute("report", report);
        if ("waitList".equals(from)) {
            model.addAttribute("op", OpView.buildInit(user, report));
        } else if ("overList".equals(from)) {
            model.addAttribute("op", OpView.buildOver(user, report));
        } else if ("waitMain".equals(from) || "over".equals(from)) {
            model.addAttribute("op", OpView.FALSE);
        } else {
            model.addAttribute("op", OpView.build(user, report));
        }
        model.addAttribute("title", "办理流程");
        return "log";
    }

    @RequestMapping("toSure")
    public String toSure(Model model, Long reportId) {
        model.addAttribute("works", userService.listWorks());
        model.addAttribute("reportId", reportId);
        return "sure";
    }

    @RequestMapping("sure")
    @ResponseBody
    public Result sure(HttpServletRequest request, Long reportId, Long mainId) {
        try {
            User user = getSessionUser(request);
            Assert.notNull(reportId, "参数错误,reportId");
            Assert.notNull(mainId, "参数错误,mainId");
            return taskService.sure(user, reportId, mainId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.byMsg(e.getMessage());
        }
    }

    @RequestMapping("toDelete")
    @ResponseBody
    public Result delete(HttpServletRequest request, Long reportId) {
        try {
            User user = getSessionUser(request);
            Assert.notNull(reportId, "参数错误,reportId");
            return taskService.delete(user, reportId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.byMsg(e.getMessage());
        }
    }

    @RequestMapping("toMyOver")
    @ResponseBody
    public Result toMyOver(HttpServletRequest request, Long reportId) {
        try {
            User user = getSessionUser(request);
            Assert.notNull(reportId, "参数错误,reportId");
            return taskService.toMyOver(user, reportId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.byMsg(e.getMessage());
        }
    }

    @RequestMapping("toOver")
    @ResponseBody
    public Result toOver(HttpServletRequest request, Long reportId) {
        try {
            User user = getSessionUser(request);
            Assert.notNull(reportId, "参数错误,reportId");
            return taskService.toOver(user, reportId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.byMsg(e.getMessage());
        }
    }

    @RequestMapping("toEvalute")
    @ResponseBody
    public Result toEvalute(HttpServletRequest request, Long reportId, String evalute) {
        try {
            User user = getSessionUser(request);
            Assert.notNull(reportId, "参数错误,reportId");
            Assert.isTrue(StringUtils.isNotBlank(evalute), "评价不能为空");
            return taskService.evalute(user, reportId, evalute);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.byMsg(e.getMessage());
        }
    }
}
